package poker;

import java.util.Random;
import java.util.ArrayList;
public class Deck
{
	private ArrayList<Card> cards;
    Deck()
    {
        cards = new ArrayList<Card>();
        for (short a=0; a<4; a++)
        {
            for (short b=0; b<13; b++)
            {
            	try
            	{
            		cards.add(new Card(a,b));
            	}
            	catch (PokerException e) 
            	{
                    System.out.println(e.getMessage());
                    e.printStackTrace();
            	}
            }
        }
        shuffle();
    }
	
	//tasowanie
	public void shuffle()
	{
		//indeksy kart do przetasowania
        int card1, card2;
		//karta pomocnicza do tasowania
        Card help;
		int size = cards.size();
		Random generator = new Random();		
		//bierzemy 2 losowe karty z talii i zamieniamy miejscami
		//czynnosc powtarzana 210 razy
        for (short i=0; i<210; i++)
        {
            card1 = generator.nextInt(size);
            card2 = generator.nextInt(size);
            help = cards.get(card2);
            cards.set(card2, cards.get(card1));
            cards.set(card1, help);
        }
	}
	
	//pobranie karty z talii
    public Card drawFromDeck()
    {       
        return cards.remove(cards.size()-1);
    }
    boolean findCard(int suit, int rank) 
    {
        Card card;
        for (int i = 0; i < cards.size(); i++) 
        {
            card = cards.get(i);
            if (card.getRank() == rank && card.getSuit() == suit) 
            {
                return true;
            }
        }
        return false;
    }
    void discardDeck() 
    {
        cards = null;
    }
}
